package com.ruoyi.energy.controller;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.energy.domain.DeviceMaintain;
import com.ruoyi.energy.domain.DpEnergyQy;
import com.ruoyi.energy.domain.NyTransAllowCheck;
import com.ruoyi.energy.domain.NyTransLoad;
import com.ruoyi.energy.domain.vo.DeviceRepairBPVo;
import com.ruoyi.energy.service.*;
import com.ruoyi.omp.api.RemoteTrendService;
import com.ruoyi.omp.api.domain.NyCheckMeter;
import com.ruoyi.omp.api.domain.TrendData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Api(tags = "大屏-智慧能源")
@RestController
@RequestMapping("/dp_energy")
public class DPEnergyController extends BaseController {

/*    @Autowired
    private INyCheckReportService nyCheckReportService;*/

    @Autowired
    private INyTransLoadService nyTransLoadService;

    @Autowired
    private IDeviceMaintainService deviceMaintainService;

    @Autowired
    private INyCheckMeterService nyCheckMeterService;

    @Autowired
    private RemoteTrendService remoteTrendService;

    @Autowired
    private IDpEnergyQyService dpEnergyQyService;

    @Autowired
    private INyTransAllowCheckService nyTransAllowCheckService;


/*
    *智慧能源-箱变负荷监控
     * transId,checkTime(month),checkDay
*/

    @ApiOperation(value="BP-箱变每日监测", notes="箱变每日监测")
    @Log(title = "BP-箱变每日监测")
    @GetMapping(value = "/listBP")
    public AjaxResult selectTransLoadByMonth(@RequestParam(value = "parkNum",required = false) String parkNum) {
        NyTransLoad nyTransLoad = new NyTransLoad();

        //某个箱变负荷还是所有箱变总负荷
        List<NyTransLoad> list = nyTransLoadService.selectNyTransLoadListPlus(nyTransLoad);
        return AjaxResult.success(list);
    }

/*
    *智慧能源-箱变负荷监控
     * transId,checkTime(month),checkDay
*/

    @ApiOperation(value="BP-箱变负荷趋势", notes="BP-箱变负荷趋势")
    @Log(title = "大屏-箱变负荷趋势")
    @GetMapping(value = "/listBPTrend")
    public AjaxResult selectTransLoadMapByMonth(@RequestParam(value = "transName") String transName) {
        int offestDay = 15;
// @RequestParam(value = "parkNum",required = false) String parkNum
/*         * 传入日期，箱变id
         * 日期前15天，
         * 返回，<号数,值>
         **/
        NyTransLoad nyTransLoad = new NyTransLoad();
        nyTransLoad.setTransName(transName);
        List<NyTransLoad> list = nyTransLoadService.selectNyTransLoadTrend(nyTransLoad);
        Map<String, Long> resultMap = new LinkedHashMap<>();
        //水表，电表 月度总额
        for(NyTransLoad nyTransLoad1:list){
            String key ="" + DateUtil.dayOfMonth(nyTransLoad1.getCheckTime());
            resultMap.put(key,nyTransLoad1.getValue());
        }
        return AjaxResult.success(resultMap);
    }

    /*    *
     * 查询能源维修次数 时间-次数
     */
/*    @ApiOperation(value="BP-能源维修次数", notes="BP-能源维修次数")
    @GetMapping("/countMonth")
    public AjaxResult countMonth(String month) {
        Map<String,Integer> list = deviceMaintainService.selectTransRepairByMonth(month);
        return AjaxResult.success(list);
    }*/

    @ApiOperation(value = "BP-设备维修记录", notes = "BP-设备维修记录")
    @GetMapping("/listBP1")
    public AjaxResult listBP1(DeviceMaintain deviceMaintain) {
        startPage();
        deviceMaintain.setModuleType("能源");
        List<DeviceMaintain> list = deviceMaintainService.selectDeviceMaintainList(deviceMaintain);
        List<DeviceRepairBPVo> result = new ArrayList<>();
        list.stream().forEach(o -> {
            DeviceRepairBPVo a = new DeviceRepairBPVo();
            a.setStation(o.getStation());
            a.setRepairTime(o.getRepairTime());
            a.setSummary(o.getSummary());
            //todo 状态
            result.add(a);
        });
        return AjaxResult.success(result);

    }

    /*
    能耗预警-设备故障-设备维修
    * */
    @ApiOperation(value = "BP-能源设备统计", notes = "BP-能源设备统计")
    @GetMapping("/faltRatio")
    public AjaxResult energyRatio() {
        String trendType = "dp_energy_device_a";
        R remote = remoteTrendService.getTrendDataByType(trendType,3);
        Map<String,String> result = (Map<String,String>)remote.getData();
        //todo 完善能源设备统计逻辑
        //Map<String,Integer> realData = deviceMaintainService.getTypeRatio();

        return AjaxResult.success(result);

    }

    /**
     *
     * @param meterType,parkNum,
     * @return
     */
    @ApiOperation(value="BP-水电月度趋势", notes="BP-水电月度趋势")
    @GetMapping("/energyTrend/{trendType}")
    public AjaxResult energyTrend(@PathVariable("trendType") String trendType, @RequestParam("parkNum") String parkNum,@RequestParam("meterType") String meterType) {
        //nyCheckMeterService.energyTrend(nyCheckMeter)
        //远程获取值，最新值 remoteTrendService
        R remote = remoteTrendService.getTrendDataByType(trendType,12);
        Map<String,String> result = (Map<String,String>)remote.getData();
        //DateUtil.dayOfMonth()
        int month = DateUtil.month(DateUtil.date());
        String dataValue = nyCheckMeterService.computeSum(parkNum,meterType,DateUtil.date()) ;
        //计算当前月份，获取值不存在插入，存在更新
        TrendData trendData = null;
        if(result.get(Integer.toString(month)) == null){
            if(dataValue != null){
                trendData = new TrendData();
                trendData.setTrendType(trendType);
                trendData.setDataLabel(Integer.toString(month));
                trendData.setDataValue(dataValue);
                remoteTrendService.addTrendData(trendData);
                result.put(trendData.getDataLabel(),trendData.getDataValue());
            }

        }else{
            if(!result.get(Integer.toString(month)).equals(dataValue)){
                if(dataValue != null){
                    remoteTrendService.updateTrendDataValue(trendType,Integer.toString(month),dataValue);
                }
            }
        }

        return AjaxResult.success(result);
    }

    /**
     *
     * @param parkNum,Feecycle,meterType
     * @return group by 企业
     */
    @ApiOperation(value="BP-企业能耗记录", notes="BP-企业能耗记录")
    @GetMapping("/energyRecord")
    public AjaxResult energyStruct(@RequestParam("parkNum") String parkNum,@RequestParam(value = "feeCycle",required = false) Date feeCycle) {
        if(feeCycle == null){
            System.out.println("feecycle is null");
            feeCycle = DateUtil.date();
            System.out.println(feeCycle);
        }
        NyCheckMeter nyCheckMeter = new NyCheckMeter();
        nyCheckMeter.setFeeCycle(feeCycle);
        if(StringUtils.isBlank(parkNum)){
            parkNum = "A";
        }
        nyCheckMeter.setParkNum(parkNum);
        DpEnergyQy dpEnergyQy = new DpEnergyQy();
        dpEnergyQy.setParkNum(parkNum);

        //return AjaxResult.success(nyCheckMeterService.energyRecord(nyCheckMeter));
        return AjaxResult.success(dpEnergyQyService.selectDpEnergyQyListPlus(dpEnergyQy));
    }

    /**
     *
     * @param parkNum,Feecycle,meterType
     * @return group by 企业
     */
    @ApiOperation(value="BP-企业能耗趋势图", notes="BP-企业能耗趋势图")
    @GetMapping("/energyRecordTrend")
    public AjaxResult energyStructTrend(@RequestParam("enterpriseName") String enterpriseName,@RequestParam("meterType") String meterType,@RequestParam(value = "parkNum",required = false) String parkNum) {

        if(StringUtils.isBlank(parkNum)){
            parkNum = "A";
        }
        DpEnergyQy dpEnergyQy = new DpEnergyQy();
        dpEnergyQy.setParkNum(parkNum);
        dpEnergyQy.setEnterpriseName(enterpriseName);
        dpEnergyQy.setMeterType(meterType);
        return AjaxResult.success(dpEnergyQyService.selectDpEnergyQyTrend(dpEnergyQy));
    }

    /**
     *
     * @param parkNum,Feecycle,meterType
     * @return group by 企业
     */
    @ApiOperation(value="BP-箱变企业用电监测", notes="BP-箱变企业用电监测")
    @GetMapping("/trans_enterprise")
    public AjaxResult transEnterpriseRecord(@RequestParam(value = "transNum" ,required = false)String transNum,@RequestParam(value = "transName" ,required = false)String transName) {
//"enterpriseName") String enterpriseName,@RequestParam("meterType") String meterType,@RequestParam(value = "parkNum",required = false) String parkNum
/*        if(StringUtils.isBlank(parkNum)){
            parkNum = "A";
        }*/
        PageHelper.startPage(1, 10);
        NyTransAllowCheck nyTransAllowCheck = new NyTransAllowCheck();
        nyTransAllowCheck.setTransName(transName);
        nyTransAllowCheck.setTransNum(transNum);
        //nyTransAllowCheck.setCheckTime(DateUtil.yesterday());
        List<NyTransAllowCheck> list = nyTransAllowCheckService.selectNyTransAllowCheckList(nyTransAllowCheck);
        return AjaxResult.success(list);
    }

    /**
     *
     * @param parkNum,Feecycle,meterType
     * @return group by 企业
     */
    @ApiOperation(value="BP-箱变企业电荷趋势", notes="BP-箱变企业电荷趋势")
    @GetMapping("/trans_enterprise_trend")
    public AjaxResult transEnterpriseRecordTrend(@RequestParam(value = "enterpriseName" ,required = false)String enterpriseName) {
        NyTransAllowCheck nyTransAllowCheck = new NyTransAllowCheck();
        nyTransAllowCheck.setEnName(enterpriseName);
        List<NyTransAllowCheck> list = nyTransAllowCheckService.selectNyTransAllowCheckList(nyTransAllowCheck);
        List<Map<String,Long>> results = new ArrayList<>();
        int offset = 12;
        NyTransAllowCheck allowCheck = null;
        for(int i = offset - 1;i>=0;i--){
            allowCheck = list.get(i);
            allowCheck.getCheckTime();
            allowCheck.getValue();
            Map map = new HashMap<>();
            map.put(DateUtil.dayOfMonth(allowCheck.getCheckTime())+"",allowCheck.getValue());
            results.add(map);
        }
        return AjaxResult.success(results);
    }
}
